package com.etc.RCCPA001;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 
 * Filename:    RCCPA001Service.java  
 * Description: 后台登录服务 
 * Copyright:   Copyright (c) 2018-2019 All Rights Reserved.
 * Company:     chinasofti.com Inc.
 * @author:     mazheng 
 * @version:    1.0  
 * Create at:   2020年3月5日 上午9:48:29  
 *  
 * Modification History:  
 * Date         Author      Version     Description  
 * ------------------------------------------------------------------  
 * 2020年3月5日      mazheng      1.0         1.0 Version  
 *
 */
@Service
public class RCCPA001Service {

    @Autowired
    RestTemplate restTemplate;

    /**
     * 登录业务处理
     * @param user
     * @return
     */
    public ResponseEntity login(RCCPA001Vo user) {
       
        String userJson = "";
        try { ObjectMapper objectMapper = new ObjectMapper();
            userJson = objectMapper.writeValueAsString(user);
        } catch (JsonProcessingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        HttpHeaders headers = new HttpHeaders();
        List<String> cookies =new ArrayList<String>();
        /* 登录获取Cookie 这里是直接给Cookie，可使用下方的login方法拿到Cookie给入*/
        cookies.add("token=Bearer-eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxMzU5MTk3NjI1NiIsImlzcyI6ImVjaGlzYW4iLCJleHAiOjE1ODM5MzE0MTcsImlhdCI6MTU4MzMyNjYxNywicm9sIjoiUk9MRV9VU0VSIn0.ICP02FFvxK52Yrk5Wx8YWzEwOBnQQqgCXJE8RTkM5zlRUU8z5jRRYuf0xzQNf4XgiqIZuqaX2gqPTbpEgav3DQ; Max-Age=10080; Expires=Wed, 04-Mar-2020 15:44:57 GMT");       //在 header 中存入cookies
        headers.put(HttpHeaders.COOKIE,cookies);        //将cookie存入头部
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        // form fangshi
//        MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();      //请求体给予内容
//        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
        HttpEntity<String> formEntity = new HttpEntity<String>(userJson, headers);
        ResponseEntity<String> response = restTemplate.postForEntity("http://SERVICE-USER-CENTER/admin/login", formEntity, String.class);
        System.out.println(response.getBody());  
        return response;
    }
}
